import React, { useState } from 'react';
import { Form, Input, Modal, Radio, TreeSelect } from 'antd';
const { TextArea } = Input;
const FormItem = Form.Item;

export interface UpdateFormProps {
  onCancel: () => void;
  onSubmit: (values: API.DepartmentItem) => void;
  updateModalVisible: boolean;
  values: API.DepartmentItem;
  treeData?: API.TreeNodeItem[];
}

const formLayout = {
  labelCol: { span: 5 },
  wrapperCol: { span: 15 },
};

const UpdateForm: React.FC<UpdateFormProps> = (props) => {
  const [formVals] = useState<API.DepartmentItem>(props.values);

  const [form] = Form.useForm();

  const { updateModalVisible, onSubmit: handleUpdate,  onCancel, treeData} = props;

  const okHandle = async () => {
    const fieldsValue = await form.validateFields();
    fieldsValue.departmentId = formVals.departmentId;
    form.resetFields();
    handleUpdate(fieldsValue);
  };

  return (
    <Modal
      width={640}
      destroyOnClose
      title="编辑部门"
      visible={updateModalVisible}
      onOk={okHandle}
      onCancel={() => onCancel()}
    >
      <Form
        {...formLayout}
        form={form}
        initialValues={{
          departmentName: formVals.departmentName,
          departmentDesc: formVals.departmentDesc,
          parentId:formVals.parentId +"",
          useState: formVals.useState
        }}
      >
        <FormItem
          label="部门名称"
          name="departmentName"
          rules={[{ required: true, message: '请输入至少二个字符！', min: 2 }]}
        >
          <Input placeholder="请输入" autoComplete='off' />
        </FormItem>
        <FormItem
          label="部门描述"
          name="departmentDesc"
         // rules={[{ required: true, message: '请输入至少十个字符！', min: 10 }]}
        >
          <TextArea  placeholder="请输入" showCount maxLength={100}  />
        </FormItem>
        <FormItem
          label="上级"
          name="parentId"
          rules={[{ required: true, message: '请选择上级！' }]}
        >
          <TreeSelect
            style={{ width: '100%' }}
            dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
            treeData={treeData}
            key="departmentId"
            placeholder="请选择上级部门"
            treeDefaultExpandAll
          />
        </FormItem>
       
        <FormItem
          label="启用状态"
          name="useState"
        >
          <Radio.Group>
            <Radio value={1} >
              启用
            </Radio>
            <Radio value={0}>
              禁用
            </Radio>
          </Radio.Group>
        </FormItem>
      </Form>
    </Modal>
  );
};

export default UpdateForm;
